<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validate-trigger="submit" :label-width="100" err-show-type="toast">
			<uni-forms-item name="order_number" label="订购数量" required>
				<uni-easyinput placeholder="订购数量" type="number" v-model="formData.order_number"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="goods_sn" label="货品编码" required>
				<uni-easyinput placeholder="货品编码" v-model="formData.goods_sn" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="name" label="货品名称" required>
				<uni-easyinput placeholder="货品名称" v-model="formData.name" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="type" label="规格型号">
				<uni-easyinput placeholder="规格型号" v-model="formData.type" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="danwei" label="计量单位">
				<uni-easyinput placeholder="单位" v-model="formData.danwei" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item required name="unit_id" :label-width="80" label="进货单位">
				<uni-data-select collection="unit" field="_id as value, name as text" v-model="formData.unit_id"
					:localdata="range"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item name="keywords" label="关键字">
				<uni-easyinput placeholder="商品关键字，为搜索引擎收录使用" v-model="formData.keywords" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="goods_desc" label="产品中文描述">
				<uni-easyinput placeholder="产品中文描述" v-model="formData.goods_desc" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="goods_date" label="订货日期">
				<uni-datetime-picker return-type="timestamp" v-model="formData.goods_date"></uni-datetime-picker>
			</uni-forms-item>
			<uni-forms-item name="yj_goods_date" label="预计发货日期">
				<uni-datetime-picker return-type="timestamp" v-model="formData.yj_goods_date"></uni-datetime-picker>
			</uni-forms-item>
			<uni-forms-item name="seller_note" label="">
				<uni-easyinput placeholder="商家备注，仅商家可见" v-model="formData.seller_note" trim="both"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/order-goods.js';

	const db = uniCloud.database();
	const dbCollectionName = 'order-goods';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"category_id": "",
				"order_number": 0,
				"name": "",
				"type": "",
				"unit_id": "",
				"danwei": "",
				"keywords": "",
				"goods_sn": "",
				"goods_desc": "",
				"goods_date": null,
				"yj_goods_date": null,
				"add_date": null,
				"last_modify_date": null,
				"seller_note": ""
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data

					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
	}
</style>